#!/bin/sh

dbhost=""
dbname="game"
dbpwd=""
datebase="account"

for i in $(seq 0 99)
do
    tab="user_account_"$i  
    
    for j in $(seq 0 99)
    do
        uidtab="userid_"$j

        echo $tab $uidtab
        sql="insert into $uidtab(uid,uuid,agency,username,agency_id) select uid,uuid,agency,username,agency_id from $tab as a where mod(uid,100)=$j and not exists (select 1 from $uidtab as b where a.uid=b.uid ) limit 10000"
        cmd="$sql;SELECT ROW_COUNT();"

        echo $cmd
        while true; do
            rows=$(mysql -h ${dbhost} -u${dbname} -p${dbpwd} -A ${datebase} -s -e "${cmd}")
            # 无数据时退出
            if [ $rows -eq 0 ]; then
                echo "$i $j, 数据已全部插入"
                break
            fi

            # 显示进度
            echo "[$(date +'%T')] 本次插入: $rows 行"
            
            # 添加延迟防止锁争用（可选）
            sleep 1
        done

    done
done


user_account_cnt=0
userid_cnt=0
for i in $(seq 0 99)
do
    sql="select count(*) cnt from "

    tab="user_account_"$i  
    tab_cnt=$(mysql -h ${dbhost} -u${dbname} -p${dbpwd} -A ${datebase} -s -e "${sql} $tab")
    user_account_cnt=`expr $user_account_cnt + $tab_cnt`
    echo "$i $tab $tab_cnt $user_account_cnt"

    uidtab="userid_"$i
    uid_cnt=$(mysql -h ${dbhost} -u${dbname} -p${dbpwd} -A ${datebase} -s -e "${sql} $uidtab")
    userid_cnt=`expr $userid_cnt + $uid_cnt`
    echo "$i $uidtab $uid_cnt $userid_cnt"
done


